$(function () {
    // 分类筛选
    sortSelFn();

    //加载数据
    loadLiveData(1);
    $("body").on('click','#searchBut',function () {//搜索
        resetScrollFn();
        loadLiveData(1);
    }).on('click','.searchbar-cancel',function () {//取消搜索
        $("#search").val('');
        resetScrollFn();
        loadLiveData(1);
    }).on('click', '#filter-order li', function () { // 排序
        $("#filter-order li").removeClass("active");
        $(this).addClass("active");
        var order = $(this).data('order');
        if(order==1){
            $(this).data("order",2);
        }else if(order==2){
            $(this).data("order",1);
        }else if(order==3){
            $(this).data("order",4);
        }else if(order==4){
            $(this).data("order",3);
        }
        $('#orderType').val(order);
        closeFilterPanel();
        resetScrollFn();
        loadLiveData(1);
    }).on('keypress','#search',function(e){
        var keycode = e.keyCode;
        if(keycode=='13') {
            e.preventDefault();
            resetScrollFn();
            loadLiveData(1);
        }
    });

    function resetScrollFn() {
        Zepto('#small-list-box').empty();
        Zepto("#page-infinite-scroll-bottom").find(".infinite-scroll").scrollTop(0);
    }
    /**
     * 加载直播数据
     * @param pageNo 页码
     */
    function loadLiveData(pageNo) {
        var params = $('#search-course-form').serializeJSON();
        params = JSON.stringify(params);
        loadMobilePathData({
            url: baseloaction + '/mobile/course/ajaxSmallClassList',
            contextId: 'small-list-box',
            pageNo: pageNo,
            pageCall: loadLiveData,
            params: params,
            callback: function (pageNo) {
            }
        });
    }

    // 分类筛选
    function sortSelFn() {
        var bEle = $('.wm-cl__wrap');
        $('.wm-buttons-select-tab__wrap').on('click', '.tab-link', function () {
            if($(this).hasClass('active')){
                if(bEle.hasClass('sortSeOpen')){
                    bEle.removeClass('sortSeOpen');
                }else {
                    bEle.addClass('sortSeOpen');
                }
            }else {
                $(this).addClass('active').siblings().removeClass('active');
                bEle.addClass('sortSeOpen');
            }
        });
        $('.modal-overlay').on('click', function () {
            if (bEle.hasClass('sortSeOpen')){
                bEle.removeClass('sortSeOpen');
            }
        });
        //初始化专业
        initSubjectData();
    }

    // 关闭筛选面板
    function closeFilterPanel() {
        $('.wm-cl__wrap').removeClass('sortSeOpen')
    }

    /**
     * 初始化专业数据
     */
    function initSubjectData() {
        $('#filter-subject-parent').html("");
        /**
         * 初始化专业
         */
        findSubjectList(function (result) {
            $('#filter-subject-parent').append('<li data-subject="">全部</li>');
            var allSubject = result;
            var parentSubjectList = allSubject[0];
            $.each(parentSubjectList,function (index,parentSubject) {
                $('#filter-subject-parent').append('<li data-subject="' + parentSubject.id + '">' + parentSubject.subjectName + '</li>');
            });
            var list = $("#filter-subject-parent li");
            if(list!=null && list.length>0){
                $(list[0]).addClass('active');
                var subjectId = $(list[0]).data("subject");
                initChildSubject(subjectId);
            }
            $("#filter-subject-parent li").click(function () {
                if ($(this).hasClass('active')) return;
                $("#filter-subject-parent li").removeClass("active");
                $(this).addClass("active");
                var subjectId = $(this).data('subject');
                initChildSubject(subjectId);
            });

            /**
             * 初始化子级科目
             * @param subjectId 父级专业ID
             */
            function initChildSubject(subjectId) {
                //$("#search-course-form>#subjectIds").val(subjectId);
                var childList = allSubject[subjectId];
                if(childList==null || childList.length<=0){
                    if(subjectId==""){
                        $("#filter-subject-son").html('<li class="col-auto" data-subject=""><a class="button button-round" href="javascript:void(0);">全部</a></li>');
                        onSubjectClick();
                    }else{
                        $("#no-data-tips").removeClass("hide");
                        $("#filter-subject-son").addClass("hide");
                    }
                }else{
                    $("#no-data-tips").addClass("hide");
                    $("#filter-subject-son").removeClass("hide");
                    $("#filter-subject-son").html('<li class="col-auto" data-subject=""><a class="button button-round" href="javascript:void(0);">全部</a></li>');
                    $.each(childList,function (index,subject) {
                        $("#filter-subject-son").append('<li class="col-auto" data-parent="' + subject.parentId + '" data-subject="' + subject.id + '">' +
                            '<a href="javascript:void(0)" class="button button-round">' + subject.subjectName + '</a>' +
                            '</li>');
                    });
                    //点击科目时
                    onSubjectClick();
                }
            }


            function  onSubjectClick(){
                $("#filter-subject-son li").click(function () {
                    if ($(this).hasClass('active')){
                        closeFilterPanel();
                        return;
                    }
                    $(this).addClass('active').siblings().removeClass('active');
                    var id = $(this).data("subject");
                    if(id){
                        if(id!=null&&id!=undefined&&id!=""){
                            $("#subjectIds").val(id);
                        }

                    }else{
                        var parentId = $(this).next('li[data-parent]').data('parent');
                        if(parentId!=null&&parentIdundefined&&parentId!=""){
                            $('#subjectIds').val(parentId);
                        }
                    }
                    closeFilterPanel();
                    loadLiveData(1);
                });
            }
        });
    }
});